<template>
	<view class="pageAll">
		<view class="pageTop">
			<view class="page-top-view">
				<image :src="labelRouge"></image>
			</view>
			<!-- 标语说不要了，先注释掉 -->
			<!-- <view class="page-top-view">
				<text>{{slogen}}</text>
			</view> -->
			<!-- 标语说不要了，先注释掉 -->
		</view>
		<view>
			<view class="authTip">
				<view class="authTipTitle">
					<text>
						申请获取以下权限
					</text>
				</view>
				<view class="authTipText">
					<text>
						获取您的公开信息（昵称，头像等）
					</text>
				</view>
			</view>
			<view class="submitView">
				<button @getuserinfo="getUserInfo" hover-class="subActive" open-type="getUserInfo">
					对小程序进行授权
				</button>
			</view>
		</view>
		<logo class="Bottomlogo"></logo>
	</view>
</template>

<script>
	import logo from '../../../components/logo/bottomLogo.vue'
	export default {
		data() {
			return {
				labelRouge: require('../../../static/index/labelRouge.png'),
				slogen: '标语的位置',
				alreadyLogin: false,
			}
		},
		methods: {
			//  获取用户微信信息
			getUserInfo(res) {
				this.$store.commit('setUserInfo', res.detail.userInfo)
				uni.login({
					provider: 'weixin',
					success: res => {
						this.login(res.code)
						// this.newInfo()
					},
					fail(e) {
						console.log('1', e);
					}
				});
			},

			//验证登录
			login(code) {
				this.$api.login({
						code: code
					})
					.then(res => {
						uni.hideLoading();
						if (res.flag == 1) {
							uni.showToast({
								title: '授权成功',
								duration: 2000
							})
							this.$store.commit('setOpenId', res.wx_user.openid)
							uni.redirectTo({
								url:'../login/login'
							})
							
						} else if (res.flag == 2) {
							uni.showToast({
								title: '授权成功',
								duration: 2000
							})
							this.$store.commit('setOpenId', res.wx_user.openid)
							uni.redirectTo({
								url:'../login/login'
							})
						} else if (res.flag == 0) { // 用户未注册
							this.newInfo(code);
						} else {
							uni.showToast({
								icon: 'none',
								title: '系统错误',
								duration: 2000
							})
						}
					})
			},

			//获取新的code iv enencryptedData
			newInfo() {
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						uni.getUserInfo({
							provider: 'weixin',
							lang: 'zh_CN',
							success: infoRes => {
								this.register(loginRes.code, infoRes.iv, infoRes.encryptedData);
							}
						});
					}
				});
			},

			//请求注册
			register(code, iv, encryptedData) {
				this.$api.registerLogin({
						code: code,
						encryptedData: encryptedData,
						iv: iv,
					})
					.then(res => {
						if (res.flag == 1) {
							this.$store.commit('setOpenId', res.wx_user.openid)
							uni.redirectTo({
								url: '../login/login'
							})
						} else if (res.flag == 2) {
							uni.showToast({
								icon: 'none',
								title: '已注册',
								duration: 2000
							})
							this.$store.commit('setOpenId', res.wx_user.openid);
							uni.redirectTo({
								url: '../login/login'
							})
						} else if (res.flag == 0) {
							uni.showToast({
								icon: 'none',
								title: '授权失败',
								duration: 2000
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '注册失败',
								duration: 2000
							})
							// 接口有bug，先这样
							// uni.redirectTo({
							// 	url: '../login/login'
							// })
						}
					})
					fail:() => {
						console.log(res, 'register')
						console.log(code, 'code')
					}
			},
		},
		components: {
			logo,
		},
		onLoad() {
		},
		onShareAppMessage: (options) => {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			var shareObj = {
				title: "红标HONBIAO", // 默认是小程序的名称(可以写slogan等)
				path: '/pages/indexPage/authorization/authorization', // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: '/static/index/labelRouge.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
				success: (res) => {
					// 转发成功之后的回调
					if (res.errMsg == 'shareAppMessage:ok') {}
				},
				fail: () => {
					// 转发失败之后的回调
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						// 用户取消转发
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				}
			};
			return shareObj
		},
	}
</script>

<style scoped>
	.pageAll {
		box-sizing: border-box;
		background-color: #fdf5f3;
		width: 100%;
		height: 100%;
		padding: 0 17upx 0 17upx;
		display: inline-block;
	}

	.pageTop {
		padding-top: 57upx;
		width: 100%;
		text-align: center;
		margin-bottom: 13.33upx;
	}

	.pageTop .page-top-view {
		margin-top: 43.33upx;
	}

	.pageTop image {
		width: 368upx;
		margin: 0 auto;
		height: 249.33upx;
	}

	.pageTop text {
		font-size: 54upx;
		color: #f72d09;
	}

	.authTip {
		margin-top: 100upx;
		text-align: center;
	}

	.authTipTitle {
		font-size: 36upx;
		color: #333333;
		font-weight: 300;
	}

	.authTipText {
		font-size: 32upx;
		color: #666666;
		padding: 10upx 0;
	}

	.submitView {
		margin-top: 340upx;
	}

	.submitView button {
		background-color: #f7533e;
		color: #FFF;
		letter-spacing: 13.33upx;
		text-indent: 13.33upx;
	}

	.subActive {
		background-color: #FFC8C7 !important;
	}

	.Bottomlogo {
		position: fixed;
		bottom: 0;
		width: 100%;
	}
</style>
